package com.jiantong.work.impl.service;

import com.google.common.base.Throwables;
import com.google.common.collect.Maps;
import com.jiantong.work.impl.dao.ImmunizationRecordDao;
import com.jiantong.work.impl.dao.ImmunizationSubRecordDao;
import com.jiantong.work.model.ImmunizationSubRecord;
import com.jiantong.work.service.ImmunizationSubRecordReadService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.PageInfo;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by mz on 26/7/17.
 *
 */
@RpcProvider
@Service
@Slf4j
public class ImmunizationSubRecordReadServiceImpl implements ImmunizationSubRecordReadService {

    private final ImmunizationRecordDao immunizationRecordDao;
    private final ImmunizationSubRecordDao immunizationSubRecordDao;

    @Autowired
    public ImmunizationSubRecordReadServiceImpl(ImmunizationRecordDao immunizationRecordDao, ImmunizationSubRecordDao immunizationSubRecordDao) {
        this.immunizationRecordDao = immunizationRecordDao;
        this.immunizationSubRecordDao = immunizationSubRecordDao;
    }

    /**
     * 根据vaccineId 查询
     *
     * @param vaccineId 主表的id
     * @return List<ImmunizationSubRecord>
     */
    @Override
    public Response<List<ImmunizationSubRecord>> selectList(Long vaccineId) {
        try{
            Map<String,Object> criteria = Maps.newHashMap();
            if(vaccineId != null){
                criteria.put("vaccineId",vaccineId);
            }

            return Response.ok(immunizationSubRecordDao.selectList(criteria));
        }catch (Exception e){
            log.error("failed to select ImmunizationSubRecord list , case = {}", Throwables.getStackTraceAsString(e));
            return Response.fail("failed to select ImmunizationSubRecord list");
        }
    }


    @Override
    public Response<Paging<ImmunizationSubRecord>> pagingImmunizationRecordGqv1(Integer pageNo, Integer pageSize, Map map) {
        try{
            PageInfo pageInfo = new PageInfo(pageNo, pageSize);

            Paging<ImmunizationSubRecord> immunizationSubRecordPaging=  immunizationSubRecordDao.pagingImmunizationRecordGqv(pageInfo.getOffset(), pageInfo.getLimit(), map);
            List <ImmunizationSubRecord>list=immunizationSubRecordPaging.getData();

             return Response.ok(immunizationSubRecordPaging);
        }catch(Exception e){
            log.error("failed to find ImmunizationDetection fromDate={},fromDate={},toDate={}),cause:{}", map,Throwables.getStackTraceAsString(e));
            return Response.fail("ImmunizationDetection.find.fail");
        }


    }


    public List<Map<String,Object>> findAlltimesAlltype(Map map){
        return immunizationSubRecordDao.findAlltimesAlltype(map);
    }

    @Override
    public Long countISRTimes(Map map) {
        return immunizationSubRecordDao.countISRTimes(map);
    }

    public List<ImmunizationSubRecord> findBybreedFactoryId(Long breedFactoryId) {
        return immunizationSubRecordDao.findBybreedFactoryId(breedFactoryId);
    }

    @Override
    public Response<Integer> countImmunizationForWeb(Map<String, Object> map) {
        try{
            return Response.ok(immunizationSubRecordDao.countImmunizationForWeb(map));
        }catch (Exception e){
            log.error("failed to count times , case = {}",Throwables.getStackTraceAsString(e));
            return Response.fail("failed to count times");
        }
    }

    @Override
    public Response<List<Map<String, Object>>> findImmunizationInfoForWeb(Map<String, Object> map) {
        try{
            return Response.ok(immunizationSubRecordDao.findImmunizationInfoForWeb(map));
        }catch (Exception e){
            log.error("failed to find immunization info , case = {}",Throwables.getStackTraceAsString(e));
            return Response.fail("failed to find immunization info");
        }
    }
}
